I need to write on a module to be run on GHCi, with a function composition to the same function. This (The classic fog(x) = f(g(x))
) runs:
(.) f g = (\x -> f (g x)).
The problem appears when I try to write it like this
(.) f f = (\x -> f (f x)). (fof(x) = f(f(x)))
GHCi says:
"Conflicting definitions for `f'
Bound at: Lab1.hs:27:9
Lab1.hs:27:12"
Line 27:9 appear on the first time f and line 27:12 appear f again.
Why doesn't Haskell understand (.) f f = (\x -> f (f x))
?
Will Ness :
In Haskell, arguments to a function must have unique names. Using the same name for another argument is not allowed. This is because\n\nfoo x y = ... === foo = (\\x-> (\\y-> ...))\n\n\nand if y where replaced with x, the second x would just shadow the first inside the ... body: there would be no way to reference the first x from there.\n\nYou can just define twice f x = f (f x):\n\n\n Prelude> :t twice \n twice :: (t -> t) -> t -> t \n Prelude> twice (+1) 4 \n 6\n\n\n\n\nAlternatively, f (f x) = (.) f f x = join (.) f x:\n\n\n Prelude Control.Monad> :t join (.) \n join (.) :: (b -> b) -> b -> b\n\n\njoin is defined in Control.Monad. For functions, it holds that join g x = g x x. It is also known as W combinator.\n\nE.g. print $ join (.) (+1) 4 prints 6.",
2014-09-22T08:03:37